﻿using System.Linq;
using System.Web.Mvc;
using Stimulus360.Services;
using Stimulus360.Services.Interfaces;
using Stimulus360.Models.Data.CustomTypes;

namespace Stimulus360.Controllers.Home
{
    [HandleError]
    public class HomeController : Controller
    {
        /// <summary>
        ///     Instance of IAnalytics service to use
        /// </summary>
        private readonly IAnalyticsService _analyticsService;

        /// <summary>
        ///     Default constructor. Creates default instance of AnalyticsService
        /// </summary>
        public HomeController()
            : this(new AnalyticsService( ))
        {
        }

        /// <summary>
        ///     Dependency Injection enabled constructor. Set ths instance of IAnalyticsService to be used
        /// </summary>
        /// <param name="service">
        ///     The IAnalytics Service to be used
        /// </param>
        public HomeController(IAnalyticsService service)
        {
            _analyticsService = service;
        }

        /// <summary>
        ///     Method redirects to the HomeView action to show the homepage
        /// </summary>
        /// <returns>
        ///     Redirects to HomeView
        /// </returns>
        public ActionResult Index()
        {
            return RedirectToAction("HomeView", "Home");
        }

        /// <summary>
        ///     Method to return the analytics data to be shown on the home page
        /// </summary>
        /// <returns>
        ///     ViewModel containing the analytics data.
        /// </returns>
        public ActionResult HomeView()
        {
            HomeViewSummary summary = _analyticsService.GetHomeViewSummary();
            return View("Index", summary);
        }


        /// <summary>
        ///     Method to return the graph data to be shown on the home page
        /// </summary>
        /// <returns>
        ///    Json representation of the graph data to be shown on the home page
        /// </returns>
        /// <remarks>
        ///     This method is called by the silverlight graph control
        /// </remarks>
        public ActionResult GraphData()
        {
            GraphData data = new GraphData
                                 {
                                     BudgetSummary = _analyticsService.GetTopNBudgetSpendSummaries("5").ToList(),
                                     IdeaTrend = _analyticsService.GetIdeationTrend().ToList(),
                                     ProjectTrend = _analyticsService.GetProjectTrend().ToList()
                                 };

            return Json(data);
        }

        /// <summary>
        ///     Method to render the About view
        /// </summary>
        /// <returns>
        ///     The About View
        /// </returns>
        public ActionResult About()
        {
            return View();
        }

        /// <summary>
        ///     Method to render the Privacy view
        /// </summary>
        /// <returns>
        ///     The privacy view
        /// </returns>
        public ActionResult Privacy()
        {
            return View();
        }

        /// <summary>
        ///     Method to render the ContactUs view
        /// </summary>
        /// <returns>
        ///     The ContactUs View
        /// </returns>
        public ActionResult Contact()
        {
            return View();
        }

        /// <summary>
        ///     Method to render the Leagl Info. view
        /// </summary>
        /// <returns>
        ///     The LegalInfo view.
        /// </returns>
        public ActionResult LegalInfo()
        {
            return View();
        }
    }
}
